package linz

fun main() {
    create {
        123
    }.map {
        "[$this]" // lambda 默认最后一行为返回值，及输出
    }.map {
        "@$this@"
    }.map {
        "-$this-"
    }.observer {
        println(this)
    }

    // todo version 2
    create2 { "hloh" }.map { 123 }.map { 798978F }.consumer { println("消费：$it") }
    // todo version 3 (T)-> R 变为 T.() -> R
    create3 { "hloh" }.map {
        length
        /**可以直接调用返回内容函数*/
    }.map { "[$this]" }.consumer { println("消费：$this") }
    // todo version 4 简化
    create4 { "hloh" }.map {
        length
        /**可以直接调用返回内容函数*/
    }.map { "[$this]" }.consumer { println("消费：$this") }


}


// 中转站
class RxJavaCoreClassObject<T>(var valueItem: T)

inline fun <I, O> RxJavaCoreClassObject<I>.map(mapAction: I.() -> O /*传入lambda进来最后lambda返回*/): RxJavaCoreClassObject<O> {
    return RxJavaCoreClassObject(mapAction(valueItem))
}

inline fun <I> RxJavaCoreClassObject<I>.observer(observerAction: I.() -> Unit) = observerAction(valueItem)


inline fun <T> create(action: () -> T) = RxJavaCoreClassObject(action())

//=============版本二==================
class Helper<T>(private var item: T) {

    fun <R> map(action: (T) -> R): Helper<R> {
        val map: R = action(item)
        return Helper(map)
    }

    fun consumer(action: (T) -> Unit) = action(item)

}

fun <R> create2(action: () -> R): Helper<R> {
    val r: R = action()
    return Helper(r)
}
//=============版本三==================

class Helper2<T>(private var item: T) {

    fun <R> map(
        action: T.()
        /** 和版本2的唯一区别*/
        -> R
    ): Helper2<R> {
        val map: R = action(item)
        return Helper2(map)
    }

    fun consumer(
        action: T.()
        /** 和版本2的唯一区别*/
        -> Unit
    ) = action(item)

}

fun <R> create3(action: () -> R): Helper2<R> {
    val r: R = action()
    return Helper2(r)
}

//=============版本三简化==================

class Helper4<T>(private var item: T) {
    fun <R> map(action: T.() -> R) = Helper4(action(item))
    fun consumer(action: T.() -> Unit) = action(item)
}
// todo err  this: ()->String
//fun <R> create4(action: () -> R) = Helper4(action)
// todo this: String
fun <R> create4(action: () -> R) = Helper4(action())
